<template>
  <el-form :model="value"
           ref="value"
           :rules="valueRules"
           label-width="80px"
           :inline="false"
           size="normal">
    <el-form-item label="企业名称"
                  prop="shortName">
      <el-input v-model="value.shortName"></el-input>
    </el-form-item>
    <el-form-item label="所属公司"
                  prop="company">
      <el-input v-model="value.company"></el-input>
    </el-form-item>
    <el-form-item label=""
                  size="normal">
      https://www.tianyancha.com(在此可查询所属公司全称及简介)
    </el-form-item>
    <el-form-item label="城市"
                  prop="province">
      <el-row :gutter="10">
        <el-col :span="12"
                :offset="0"
                style="width:400px">
          <el-select v-model="value.province"
                     @change="getCitys"
                     placeholder="请选择"
                     size='small'>
            <el-option v-for="(item,index) in provincesList"
                       :key="index"
                       :label="item"
                       :value="item">
            </el-option>
          </el-select>
        </el-col>
        <el-col :span="12"
                :offset="0"
                style="width:400px">
          <el-form-item label=""
                        prop="city">
            <el-select v-model="value.city"
                       placeholder="请选择"
                       size='small'>
              <el-option v-for="(item,index) in citysList"
                         :key="index"
                         :label="item"
                         :value="item">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form-item>

    <el-form-item label="方向(企业标签)"
                  prop="tags">
      <el-input v-model="value.tags"></el-input>
    </el-form-item>
    <el-form-item label="备注"
                  prop="remarks">
      <el-input v-model="value.remarks"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary"
                 @click="onSubmit">立即创建</el-button>
      <el-button @click="$emit('close')">取消</el-button>
    </el-form-item>
  </el-form>

</template>
<script>
import { provinces, citys } from '@/api/hmmm/citys'
import { update, add } from '@/api/hmmm/companys'
export default {
  name: 'CompanysAdd',
  props: {
    value: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      provincesList: provinces(),
      citysList: [],
      valueRules: {
        shortName: [{ required: true, message: '请输入活动名称', trigger: 'blur' }],
        company: [{ required: true, message: '请输入活动名称', trigger: 'blur' }],
        province: [{ required: true, message: '请输入活动名称', trigger: 'blur' }],
        city: [{ required: true, message: '请输入活动名称', trigger: 'blur' }],
        tags: [{ required: true, message: '请输入活动名称', trigger: 'blur' }],
        remarks: [{ required: true, message: '请输入活动名称', trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 获取市内区域列表
    getCitys(value) {
      console.log(value)
      this.citysList = citys(value)
    },
    async onSubmit() {
      if (!this.value.id) {
        // 发送增加请求
        try {
          this.value.isFamous = true
          await add(this.value)
          this.$emit('close')
        } catch (error) {
          console.log(error)
        }
      } else {
        try {
          // 发送修改请求
          this.value.isFamous = this.value.isFamous == 1 ? true : false
          await update(this.value)
          this.$emit('input', this.value)
          this.$emit('close')
        } catch (error) {
          console.log(error)
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
